using UnityEngine;

public class CameraShakeController : MonoBehaviour
{
	private static CameraShakeController _instance;

	public MenuVisualEffectController menuVisualEffect;

	private Rigidbody rig;

	public static CameraShakeController Instance => _instance;

	private void Awake()
	{
		if (_instance != null)
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
		else
		{
			_instance = this;
		}
	}

	private void Start()
	{
		rig = GetComponent<Rigidbody>();
		rig.maxAngularVelocity = 10000f;
	}

	private void FixedUpdate()
	{
		if (Time.timeScale < 1f)
		{
			rig.velocity *= 0.95f;
			rig.angularVelocity *= 0.95f;
		}
		float d = Vector3.Angle(base.transform.forward, Vector3.right);
		Vector3 a = Vector3.Cross(base.transform.forward, Vector3.right);
		rig.AddTorque(a * d * 100000f * Time.fixedDeltaTime, ForceMode.Acceleration);
	}

	public void AddShake(Vector3 direction)
	{
		if (!menuVisualEffect.visualObject.activeSelf)
		{
			direction *= 0.01f;
			direction *= 5f;
			float magnitude = direction.magnitude;
			direction.x = 0f;
			direction = direction.normalized * magnitude;
			float d = 1f;
			if (rig.velocity.magnitude > 0f && rig.velocity.magnitude * 10f > 1f)
			{
				d = 1f / (rig.velocity.magnitude * 10f);
			}
			rig.AddForce(direction * d * 100f, ForceMode.VelocityChange);
		}
	}
}
